package cn.csg.ams.collect2.poptopics.dataHandle;

import cn.csg.ams.collect2.poptopics.datahub.TupleRecordDataUtils;
import cn.csg.ams.collect2.poptopics.entity.LoadData;
import cn.csg.ams.collect2.poptopics.entity.RecordInfo;
import cn.csg.ams.collect2.poptopics.service.MkMcTermAlarmLogService;
import com.aliyun.datahub.client.model.TupleRecordData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;

/**
 * 电流电压
 */
@Component
public class LoadHandle {

    private static Logger logger = LoggerFactory.getLogger(LoadHandle.class);

    @Autowired
    private MkMcTermAlarmLogService alarmLogService;

    /**
     * 停复电处理类
     */
    @Autowired
    private OutgaeAndRecover outgaeAndRecover;

    // @PostConstruct
    // public void test(){
    //     TupleRecordData data = TupleRecordDataUtils.getCurAndVlotSchema();
    //     handle(data);
    // }


    public void handle(TupleRecordData data) {
        // 开始时间
        long startTimes = System.currentTimeMillis();

        // 运行电能表标识
        String runMeterId = TupleRecordDataUtils.getStringField(data, "run_meter_id");
        // 地区编码
        String areaCode = TupleRecordDataUtils.getStringField(data, "area_code");

        // 查询对应档案信息
        RecordInfo recordInfo = alarmLogService.getRecordInfo(runMeterId, areaCode);
        if (recordInfo == null || recordInfo.getSupplyOrgNo() == null) {
            // 未查询到档案信息，直接返回
            logger.info("无档案，耗时：" + (System.currentTimeMillis() - startTimes));
            return;
        }

        // A相电压
        BigDecimal voltA = TupleRecordDataUtils.getBigDecimalField(data, "p_volt_a");
        // A相电压
        BigDecimal voltB = TupleRecordDataUtils.getBigDecimalField(data, "p_volt_b");
        // A相电压
        BigDecimal voltC = TupleRecordDataUtils.getBigDecimalField(data, "p_volt_c");
        // A相电流
        BigDecimal currA = TupleRecordDataUtils.getBigDecimalField(data, "p_curr_a");
        // A相电流
        BigDecimal currB = TupleRecordDataUtils.getBigDecimalField(data, "p_curr_b");
        // A相电流
        BigDecimal currC = TupleRecordDataUtils.getBigDecimalField(data, "p_curr_c");

        // 电能表接线方式
        String wiringMode = recordInfo.getWiringMode();

        // 通过电流电压判断是否复电
        Optional<Boolean> isTd = LoadData.isTd(voltA, voltB, voltC, currA, currB, currC, wiringMode);
        if (!isTd.isPresent() || isTd.get()) {
            logger.info("无电流电压，耗时：" + (System.currentTimeMillis() - startTimes));
            return;
        }

        // 暂时先set一下，档案中的字段后来加的，redis中没有
        recordInfo.setAreaCode(areaCode);
        // 停电结束时间
        long outageEndTimes = TupleRecordDataUtils.getLongField(data, "data_time") / 1000;
        String outageEndTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(outageEndTimes));
        outgaeAndRecover.otherRecover(outageEndTime, "5",  recordInfo, startTimes);
    }

}
